package pro.bbs.server.web;

import com.yy.kit.collections.YYListKits;
import com.yy.kit.collections.YYMapKits;
import com.yy.kit.random.YYRandomKits;
import com.yy.kit.secure.YYSecureKits;
import com.yy.kit.string.YYStrKits;
import com.yy.pojo.base.TUser;
import com.yy.pojo.base.TUserExample;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pro.bbs.server.mapper.base.TUserMapper;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 论坛用户登陆，注销相关Controller
 * </p>
 *
 * @author 杨阳
 * @since 2017年10月19日
 */
@RestController
@Api(value = "论坛服务端，登陆注册登陆相关的controller", tags = "论坛服务端，登陆注册登陆相关的controller")
public class BbsServerLoginController {


    private TUserMapper tUserMapper;

    @Autowired
    public BbsServerLoginController(TUserMapper tUserMapper) {
        this.tUserMapper = tUserMapper;
    }

    /**
     * <p>
     * 用户注册
     * </p>
     *
     * @param user {@link TUser}
     * @return {@link Map<String, Object>}
     */
    @ApiOperation(value = "论坛用户注册", httpMethod = "POST", tags = "论坛用户注册，登陆名就是邮箱，密码与邮箱相同")
    @PostMapping("/userRegister")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "注册的用户信息", name = "user", paramType = "form", dataType = "com.yy.pojo.base.TUser", required = true)
    })
    public Map<String, Object> userRegister(@RequestBody TUser user) {
        Map<String, Object> map = YYMapKits.createHashMap();
        //1.查看用户邮箱是否已经存在
        TUserExample example = new TUserExample();
        example.createCriteria().andEmailEqualTo(user.getEmail());
        long el = tUserMapper.countByExample(example);
        if (el > 0) {
            map.put("success", Boolean.FALSE);
            map.put("msg", "邮箱地址重复!");
            return map;
        }

        //判断昵称是否重复
        example.createCriteria().andNameEqualTo(user.getName());
        el = tUserMapper.countByExample(example);
        if (el > 0) {
            map.put("success", Boolean.FALSE);
            map.put("msg", "昵称重复!");
            return map;
        }

        user.setId(YYStrKits.getTableId());
        //先随便给个10位的随机数吧
        user.setUser_num(Long.parseLong(YYRandomKits.randomNumbers(10)));
        user.setPassword(YYSecureKits.md5(user.getPassword()));
        //默认就是男
        user.setSex(1);
        //帐号就是邮箱
        user.setAccount(user.getEmail());
        user.setStatus(1);
        user.setCreate_date(new Date());
        user.setFolk("汉族");

        int b = tUserMapper.insertSelective(user);
        if (b > 0) {
            map.put("success", Boolean.FALSE);
            map.put("msg", "注册失败，请重试!");
        } else {
            map.put("success", Boolean.TRUE);
            map.put("msg", "注册成功!");
        }

        return map;
    }

    /**
     * <p>
     * 论坛用户登陆，论坛是使用用邮箱以及密码登录
     * </p>
     *
     * @param user    {@link TUser}
     * @param session {@link HttpSession}
     * @return {@link Map<String, Object>}
     */
    @RequestMapping(value = "/remoteUserLogin", method = RequestMethod.POST)
    public Map<String, Object> remoteUserLogin(@RequestBody TUser user, HttpSession session) {
        Map<String, Object> map = YYMapKits.createHashMap();
        String email = user.getEmail();
        String password = user.getPassword();

        TUserExample example = new TUserExample();
        example.createCriteria().andEmailEqualTo(email);

        List<TUser> userList = tUserMapper.selectByExample(example);

        if (YYListKits.isEmpty(userList)) {
            map.put("success", Boolean.FALSE);
            map.put("msg", "邮箱不存在,请重新输入!");
            return map;
        } else {
            TUser tUser = userList.get(0);
            String pwd = tUser.getPassword();
            if (!pwd.equals(YYSecureKits.md5(password))) {
                map.put("success", Boolean.FALSE);
                map.put("msg", "密码错误,请重新输入!");
                return map;
            } else {//登陆成功
                map.put("success", Boolean.TRUE);
                map.put("msg", "登陆成功!");
                map.put("tuser", tUser);
                return map;
            }
        }
    }

}
